//
//  SZLCreateAddressViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/10.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLCreateAddressViewController.h"
#import "ReceiveAddressNetwork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "VefityTool.h"

@interface SZLCreateAddressViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *tellField;
@property (weak, nonatomic) IBOutlet UITextField *addressField;

@end

@implementation SZLCreateAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"新建收货地址";
    if (self.model) {
        self.nameField.text = self.model.receivePerson;
        self.tellField.text = self.model.receivePersonPhone;
        self.addressField.text = self.model.receiveAddress;
    }
}
- (IBAction)saveClick:(UIButton *)btn
{
    [self.view endEditing:YES];
    if (self.nameField.text.length<1) {
        [MBProgressHUD showError:@"请填写名字" toView:self.view];
        return;
    }
    if (![VefityTool isValidTellNumber:self.tellField.text]) {
        [MBProgressHUD showError:@"请填写正确的手机号码" toView:self.view];
        return;
    }
    if (self.addressField.text.length<1) {
        [MBProgressHUD showError:@"请填写详细地址" toView:self.view];
        return;
    }
    if (self.model) {
        [self modifyAddress];
    }else{
        [self createAddress];
    }
}
- (void)modifyAddress
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"id"] = @(self.model.ID);
    parame[@"receivePerson"] = self.nameField.text;
    parame[@"receivePersonPhone"] = self.tellField.text;
    parame[@"receiveAddress"] = self.addressField.text;
    [self showWaiter];
    [ReceiveAddressNetwork modifyDetailAddressWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"修改成功" toView:self.view];
            if (self.saveSuccessBlock) {
                self.saveSuccessBlock();
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)createAddress
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"receivePerson"] = self.nameField.text;
    parame[@"receivePersonPhone"] = self.tellField.text;
    parame[@"receiveAddress"] = self.addressField.text;
    [self showWaiter];
    [ReceiveAddressNetwork createAddressWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"创建成功" toView:self.view];
            if (self.saveSuccessBlock) {
                self.saveSuccessBlock();
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
@end
